const test_baseDmain = "http://121.36.197.246:11112"
const cloudPath = '/container-trashrecycle-core'
const getOpenId = (code)=>{
  return new Promise((resolve,reject)=>{
      wx.request({
        url: test_baseDmain+"/api/sys/core/user/login",
        data:{
          tag:0,
          code
        },
        success:res=>
        {
          resolve(res)
        },
        fail:err=>
        {
          reject(err)
        }
      })
  })
}
const addUser = (nickname,userId,imageUrl,integration,openId,orderNum,sessionKey,trashWeight)=>
{ 
  return new Promise(
    (resolve,reject)=>
    {
      wx.request({
        url: test_baseDmain+'/api/sys/core/user/add',
        method:"POST",
        data:{
          imageUrl,
          integration,
          nickname,
          openId,orderNum,sessionKey,
          trashWeight,
          userId
        },
        success:res=>
        {
          resolve(res)
        },
        fail:err=>
        {
          reject(err)
        }
      })
    }
  )
}
const getUserById = (openid)=>{
  return new Promise(
    (resolve,reject)=>
    {
      wx.request({
        url: test_baseDmain+'/api/sys/core/user/sample',
        method:"GET",
        data:{
          id:openid
        },
        success:res=>
        {
          resolve(res)
        },
        fail:err=>
        {
          reject(err)
        }
      })
    }
  )
}
const userInterface = (imageUrl,integration,nickname,openId,orderNum,sessionKey,trashWeight,userId,type)=>{
  return new Promise((resolve,reject)=>
  {
    let method,url
    switch(type)
    {
      case 'add':
        method="POST"
        url="/api/sys/core/user/add"
        break;
      case 'update':
        method="POST",
        url="/api/sys/core/user/saveorupdate"
        break
    }
    wx.request({
      url: test_baseDmain+url,
      method:method,
      data:{
          imageUrl,
          integration,
          nickname,
          openId
          ,orderNum,sessionKey,
          trashWeight,
          userId
      },
      success:res=>
      {
        resolve(res)
      },
      fail:err=>
      {
        reject(err)
      }
    })
  }
)
}


const addEasy = (blogId,userId,content,imageUrl,comment,thumb,time)=>{
  // return new Promise(
  //   (resolve,reject)=>
  //   {
      // wx.request({
        return wx.cloud.callContainer({
        // url: test_baseDmain+'/api/sys/core/user/add',
        path:cloudPath+'/api/sys/core/user/add',
        method:"POST",
        data:{
          blogId,
          comment,
          content,
          imageUrl,
          thumb,
          time,
          title,
          userId
        }
        // ,
        // success:res=>
        // {
        //   resolve(res)
        // },
        // fail:err=>
        // {
        //   reject(err)
        // }
  //   }
  // )
})
}

const AppointOrderInterface  = (addrNo,courierName,courierTel,integration,isClassified,items,orderId,orderStatus,senderAddress,senderDateBegin,senderDateEnd,senderName,senderRemark,senderTel,senderUrgent,senderWeight,time,userId,type)=>
{

  return new Promise((resolve,reject)=>
  {
    let method,url
    switch(type)
    {
      case 'add':
        method="POST"
        url="/api/sys/core/appointorder/add"
        break;
      case 'getAll':
        method="POST"
        url="/api/sys/core/appointorder/filter/list"
        break;
    }

    wx.request({
      url: test_baseDmain+url,
      method:method,
      data:{
        addrNo,courierName,courierTel,integration,isClassified,items,orderId,orderStatus,senderAddress,senderDateBegin,senderDateEnd,senderName,senderRemark,senderTel,senderUrgent,senderWeight,time,userId
      },
      success:res=>
      {
        resolve(res)
      },
      fail:err=>
      {
        reject(err)
      }
  }
  )
}
  )}

  const getAllAppointOrder = (category,courierName,courierTel,integration,orderId,orderStatus,senderAddress,senderDate,senderName,senderRemark,senderTel,senderUrgent,senderWeight,time,userId)=>
  {
    return new Promise((resolve,reject)=>
    {
      wx.request({
        url: test_baseDmain+'/api/sys/core/appointorder/list',
        method:"POST",
        data:{
          category,courierName,courierTel,integration,orderId,orderStatus,senderAddress,senderDate,senderName,senderRemark,senderTel,senderUrgent,senderWeight,time,userId
        },
        success:res=>
        {
          resolve(res)
        },
        fail:err=>
        {
          reject(err)
        }
    }
    )
  }
    )}

    const deliverorderInterface = (addrNo,category,feedback,integration,orderId,orderStatus,place,time,userId,weight,type) =>
    {
      return new Promise((resolve,reject)=>
      {
        var method;
        var url;
        switch (type) {
          case 'add':         
            method="POST"
            url="/api/sys/core/deliverorder/add"
            break;
          case 'getAll':
            method="POST"
            url="/api/sys/core/deliverorder/filter/list"
            break;
        }

        
        wx.request({
          url: test_baseDmain+url,
          method:method,
          data:{
            addrNo,category,feedback,integration,orderId,orderStatus,place,time,userId,weight
          },
          success:res=>
          {
            resolve(res)
          },
          fail:err=>
          {
            reject(err)
          }
      })
    })
  }

  const communityInterface  =(blogId,comment,content,imageUrl,thumb,time,title,userId,type)=>
  {
    return new Promise((resolve,reject)=>
    {
      var method;
      var url;
      switch (type) {
        case 'add':         
        method="POST"
        url="/api/sys/core/community/add"
        break;
      case 'getAll':
        method="POST"
        url="/api/sys/core/community/filter/list"
        break;
      case 'update':
        method="POST"
        url="/api/sys/core/community/saveorupdate"
        break;
      }

      wx.request({
        url: test_baseDmain+url,
        method:method,
        data:{blogId,comment,content,imageUrl,thumb,time,title,userId},
      success:res=>
      {
        resolve(res)
      },
      fail:err=>
      {
        reject(err)
      }
    })
  })
  }

  const uploadImg = function(filePath){
    return new Promise((resolve,reject)=>{
          wx.uploadFile({
            filePath: filePath,
            name: 'file',
            url:test_baseDmain+'/api/sys/core/community/image/upload',
            formData: {
              'user':"app.globalData.userInfo.openId"
            },
            success: (result) => {
              // console.log(result)
              resolve(JSON.parse(result.data).data.value)
            },
            fail: (res) => {},
            complete: (res) => {},
          })


        /**
         * post上传图片
         */
        // wx.request({
        //   url: test_baseDmain+'/api/sys/core/community/image/upload',
        //   method:"POST",
        //   data:{
        //     file:filePath
        //   }
        // })
    })

}

const thumbInterface = (blogId,userId,nickname="123",type)=>{
  return new Promise((resolve,reject)=>{
    let method,url
    switch(type)
    {
      case 'getIsThumb':
        method="POST"
        url="/api/sys/core/thumb/filter/sample"
        break;
      case 'addThumb':
        method="POST"
        url="/api/sys/core/thumb/add"
        break;
    }
    wx.request({
      url: test_baseDmain+url,
      method:method,
      data:{blogId,userId,nickname},
      success:res=>{resolve(res)},
      fail:err=>{reject(err)}
    })
  })
}

const deleteThumb = (id)=>{
  return new Promise((resolve,reject)=>{
    wx.request({
      url: test_baseDmain+`/api/sys/core/thumb/delete?id=${id}`,
      method:"DELETE",
      success:res=>{resolve(res)},
      fail:err=>{reject(err)}
    })
  })
}
const commentInterface = (commentId,blogId,userId,content,nickname,isChecked,commentUserId,type)=>{
  return new Promise((resolve,reject)=>{
    let method,url
    switch(type)
    {
      case 'getCommentListByblogId':
        method="POST"
        url="/api/sys/core/comment/filter/list"
        break;
      case 'addComment':
        method="POST"
        url="/api/sys/core/comment/add"
        break;
    }
    wx.request({
      url: test_baseDmain+url,
      method:method,
      data:{commentId,blogId,userId,content,nickname,isChecked,commentUserId},
      success:res=>{resolve(res)},
      fail:err=>{reject(err)}
    })
  })
}

const noticeUserInterface = (id,isChecked,systemNoticeId,userId,type)=>{
  return new Promise((resolve,reject)=>{
    let method,url
    switch(type)
    {
      case 'getAllNotice':
        method="POST"
        url="/api/sys/core/notice-user/filter/list"
        break;
      case 'add':
        method="POST"
        url="/api/sys/core/notice-user/add"
        break;
    }
    wx.request({
      url: test_baseDmain+url,
      method:method,
      data:{id,isChecked,systemNoticeId,userId},
      success:res=>{resolve(res)},
      fail:err=>{reject(err)}
    })
  })
}
const noticeInterface = (systemNoticeId,content,type)=>{
  return new Promise((resolve,reject)=>{
    let method,url
    switch(type)
    {
      case 'add':
        method="POST"
        url="/api/sys/core/notice/add"
        break;
      case 'getNoticeContent':
        method="GET"
        url="/api/sys/core/notice/sample"
    }
    wx.request({
      url: test_baseDmain+url,
      method:method,
      data:{id:systemNoticeId,content},
      success:res=>{resolve(res)},
      fail:err=>{reject(err)}
    })
  })
}
const getimgByBlogId = (blogId)=>{
  return new Promise((resolve,reject)=>{
    wx.request({
      url: test_baseDmain+"/api/sys/core/community/image/url",
      method:"GET",
      data:{id:blogId},
      success:res=>{resolve(res)},
      fail:err=>{reject(err)}
    })
  })
}
module.exports = {
  addUser,
  getUserById,
  userInterface,
  addEasy,
  AppointOrderInterface,
  getAllAppointOrder,
  deliverorderInterface,
  communityInterface,
  uploadImg,
  getOpenId,
  thumbInterface,
  deleteThumb,
  commentInterface,
  noticeUserInterface,
  noticeInterface,
  getimgByBlogId
}